package com.huazai.apply.manager.dao.impl;



import com.huazai.apply.manager.dao.UserDao;
import com.huazai.apply.manager.utils.DbUtil;
import com.huazai.apply.manager.utils.IdUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoReal implements UserDao {
    /**
     * 数据库层来判断是够登录成功
     *
     * @param userid
     * @param password
     * @param field 这个状态是群分是管理员还是普通用户
     * @return 登录状态
     */
    @Override
    public boolean login(String userid, String password ,String field)
    {
        String sql = "select Cname from userinfo where Cid =? and Cpassword =? and Cfield =?";
        boolean res = false;
        try {
            Connection conn = DbUtil.getConnectDB();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, userid);
            ps.setString(2, password);
            ps.setString(3,field);
            ResultSet rs = ps.executeQuery();
            res = rs.next();
            DbUtil.close(conn, ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean signup(String username, String userid, String password, String field) {
        String sql = "insert into userinfo(Cname,Cid,Cpassword,Cfield) values(?,?,?,?)";
        boolean res = false;
        try {
            Connection conn = DbUtil.getConnectDB();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, userid);
            ps.setString(3, password);
            ps.setString(4, field);
            if ( ps.executeUpdate() != 0 ) {
                res = true;
            }
            DbUtil.close(conn, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean checkUsername(String userid, String userField) {
        String sql = "select * from userinfo where Cid = ? and Cfield = ?";
        boolean res = false;
        try {
            Connection conn = DbUtil.getConnectDB();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, userid);
            ps.setString(2, userField);
            ResultSet rs = ps.executeQuery();
            res = rs.next();
            DbUtil.close(conn, ps, rs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean updateSubmit(String applyId, String result) {
        boolean res = false;

        String sql = "update submitinfo set Hresult = ? where applyID = ?";
        try {
            Connection conn = DbUtil.getConnectDB();
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,result);
            ps.setString(2, applyId);

            int ans = ps.executeUpdate();
            res = (ans > 0);
            DbUtil.close(conn, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean deleteSubmit(String applyId) {
        boolean res = false;
        Connection conn = null;
        String sql = "delete from submitinfo where applyID = ?";
        try {
            conn = DbUtil.getConnectDB();
            conn.setAutoCommit(false);
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, applyId);
            if ( ps.executeUpdate() != 0 ) {
                res = true;
            }
            conn.commit();
            DbUtil.close(conn, ps, null);
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public boolean uploadSubmit(String userid, String submitDate, int wantTime, String result) {
        boolean res = false;
        String sql = "insert into submitinfo(applyID,Huserid,Hsubmit,Htime,Hresult) values(?,?,?,?,?)";
        try {
            IdUtil idWorker = new IdUtil(1, 1, 1);
            Connection conn = DbUtil.getConnectDB();
            PreparedStatement ps = conn.prepareStatement(sql);

            ps.setLong(1,idWorker.nextId());
            ps.setString(2, userid);
            ps.setString(3, submitDate);
            ps.setInt(4,wantTime);
            ps.setString(5,result);

            if ( ps.executeUpdate() != 0 ) {
                res = true;
            }
            DbUtil.close(conn, ps, null);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return res;
    }

}
